package use_ndk;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.toolbox.Volley;
import com.example.com.skills_utf8.R;
import com.utils.AppUtils;
import com.utils.UserService;
import com.utils.Utils1;
import com.utils.cHighTimer;
import com.utils.g;

// http://blog.csdn.net/xiaocai237/article/details/6669705

public class _37ShakeActivity extends Activity {
	
	
	static{
		System.loadLibrary("k");
	}
	

	
	private final String TAG = _37ShakeActivity.class.getSimpleName();
	
	
	private com.android.volley.RequestQueue requestQueue;
	
	
	
	
	_37ShakeActivity activity = this;

	private TextView tv_shake, tv_content, tv_count;
	private Button btn_left;

	Sensor mAccSensor;
	private SensorManager sm;
	private Vibrator mVibrator;

	AccListener mAccListener = new AccListener();;
	cHighTimer mTimer = new cHighTimer();

	private MediaPlayer s_Sucess;

	boolean bHadShake;


	// dialog
	// private TextView tv_shake_item_name;

	int shark_count = 0;

	
	private float Toggle = 1.5f;  //触发值
	
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_shake);

		
		requestQueue = Volley.newRequestQueue(this);  
		
		

		sm = (SensorManager) getSystemService(SENSOR_SERVICE);

		// 获取加速度传感器
		mAccSensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

		// 获得震动控制器
		mVibrator = (Vibrator) getSystemService(this.VIBRATOR_SERVICE);

		tv_shake = (TextView) findViewById(R.id.tv_shake);
		btn_left = (Button) findViewById(R.id.btn_left);
		tv_content = (TextView) findViewById(R.id.tv_content);
		tv_count = (TextView) findViewById(R.id.tv_count);

		mTimer.start_counter();

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		// mAccListener = new AccListener();
		sm.registerListener(mAccListener, mAccSensor,
				SensorManager.SENSOR_DELAY_GAME);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		sm.unregisterListener(mAccListener);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();

		bHadShake = false;
	}

	public void OnShake() {

		System.out.println("OnShake");

		if (!bHadShake) {
			bHadShake = true;


		//	if (g.DEBUG)
				Utils1.showToastShort(activity, "摇一摇~");


		}

	}

	

	// 采集一段时间内, x轴的加速度变化量

	class AccListener implements SensorEventListener {

		List<Float> listdata = new ArrayList<Float>();

		private float old_x_acc;
		private float old_y_acc;
		private float old_z_acc;

		private long FirstTime = 0;
		private long mDeltime = 0;

		@Override
		public void onSensorChanged(SensorEvent event) {
			// TODO Auto-generated method stub

			// 系统给的能达到的是20ms每帧

			float elapsed = (event.timestamp - FirstTime) / (1000 * 1000);
			System.out.println("timestamp: " + elapsed + "ms");
			FirstTime = event.timestamp;


			float x_acc = event.values[0];
			float y_acc = event.values[1];
			float z_acc = event.values[2];

			/*
			 * if(z_acc < -8){ Utils1.showToastShort(activity, "手机背面朝上~"); }
			 */

			float delta_x_acc = x_acc - old_x_acc;
			float delta_y_acc = y_acc - old_y_acc;
			float delta_z_acc = z_acc - old_z_acc;


			listdata.add(delta_x_acc); 
			if (listdata.size() > 50) { // 采样1s中
				
				Float[] array = new Float[listdata.size()];
				Float[] t = listdata.toArray(array);
				boolean bShake = JNI.isShake( t );
				if(bShake)
					OnShake();
				
				/*
				float sum_delta = 0;
				int size = listdata.size();
				for (int i = 0; i < size; i++) {
					float t = listdata.get(i);
					sum_delta += Math.abs(t);

					if (g.DEBUG)
						System.out.println(t);
				}

				if (g.DEBUG) {
					System.out.println("result: " + sum_delta);
					System.out.println("size: " + size);
				}

				float result = sum_delta / size; // 取平均值
				if (result > Toggle) { // delta值能够触发

					OnShake();

				}
				if (g.DEBUG)
					System.out.println("result: " + result);
				*/
				

				listdata.clear();
			}

			old_x_acc = x_acc;
			old_y_acc = y_acc;
			old_z_acc = z_acc;

		}

		@Override
		public void onAccuracyChanged(Sensor sensor, int accuracy) {
			// TODO Auto-generated method stub

		}

	}


};

//

/*
 * 各传感器 04-27 20:50:56.320: I/System.out(19549): LSM330DLC 3-axis Accelerometer
 * 04-27 20:50:56.320: I/System.out(19549): AK8975C 3-axis Magnetic field sensor
 * 04-27 20:50:56.320: I/System.out(19549): iNemoEngine Orientation sensor 04-27
 * 20:50:56.320: I/System.out(19549): CM36651 Light sensor 04-27 20:50:56.320:
 * I/System.out(19549): CM36651 Proximity sensor 04-27 20:50:56.320:
 * I/System.out(19549): LSM330DLC Gyroscope sensor 04-27 20:50:56.320:
 * I/System.out(19549): iNemoEngine Gravity sensor 04-27 20:50:56.320:
 * I/System.out(19549): iNemoEngine Linear Acceleration sensor 04-27
 * 20:50:56.320: I/System.out(19549): iNemoEngine Rotation_Vector sensor 04-27
 * 20:50:56.320: I/System.out(19549): LPS331AP Pressure Sensor 04-27
 * 20:50:56.320: I/System.out(19549): Rotation Vector Sensor 04-27 20:50:56.320:
 * I/System.out(19549): Gravity Sensor 04-27 20:50:56.320: I/System.out(19549):
 * Linear Acceleration Sensor 04-27 20:50:56.320: I/System.out(19549):
 * Orientation Sensor 04-27 20:50:56.320: I/System.out(19549): Corrected
 * Gyroscope Sensor
 */

